package com.ztesoft.ossup.report.web.chart;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ztesoft.ossup.report.domain.Report;
import com.ztesoft.ossup.report.domain.ReportSub;
import com.ztesoft.ossup.report.service.ChartConfigService;
import com.ztesoft.ossup.report.service.DataPreviewService;
import com.ztesoft.ossup.report.utils.Constants;
import com.ztesoft.ossup.report.web.BaseController;

@Controller
@RequestMapping("/report/chartconfig")
public class ChartConfigController extends BaseController {

	@Autowired
	ChartConfigService chartConfigService;
	
	@Autowired
	DataPreviewService dataPreviewService;
	
	@RequestMapping(value = "/toAddChartConfig/{reportId}", method = RequestMethod.GET)
	public String toAddChartConfig(@PathVariable Long reportId, HttpServletRequest request) {
		Report report = dataPreviewService.getReportById(reportId);
		request.setAttribute("reportId", reportId);
		request.setAttribute("reportCode", report.getCode());
		request.setAttribute("lineChart", "0");//类型初始化
		request.setAttribute("barChart", "0");
		request.setAttribute("pieChart", "0");
		List<ReportSub> reportSubs = chartConfigService.getReportSub(reportId);
		for (ReportSub reportSub : reportSubs) {
			if (reportSub.getType().equals(Constants.CHART_TYPE_LINE)){//曲线图
				request.setAttribute("lineChart", Constants.CHART_TYPE_LINE);
			}else if (reportSub.getType().equals(Constants.CHART_TYPE_BAR)){//柱状图
				request.setAttribute("barChart", Constants.CHART_TYPE_BAR);
			}else if (reportSub.getType().equals(Constants.CHART_TYPE_PIE)){//饼状图
				request.setAttribute("pieChart", Constants.CHART_TYPE_PIE);
			}
		}
		return "/chart/addChartConfig";
	}

	@RequestMapping(value = "/addChartConfig", method = RequestMethod.POST)
	@ResponseBody
	public String addChartConfig(@RequestBody Map<String, Object> params) {

		ResponseEntry responseEntry = new ResponseEntry(SUCCESS);
		Long chartId=chartConfigService.addChart(params);
		if (chartId==0) {
			responseEntry.setCode(FAIL);
		}
		return responseEntry.getJSON();
	}

}
